package com.xunqi.mall.service.impl;

import com.xunqi.mall.consts.MallConst;
import com.xunqi.mall.enums.ResponseEnum;
import com.xunqi.mall.form.UserLoginForm;
import com.xunqi.mall.form.UserRegisterForm;
import com.xunqi.mall.mapper.UserMapper;
import com.xunqi.mall.pojo.User;
import com.xunqi.mall.service.UserService;
import com.xunqi.mall.vo.ResponseVo;
import com.xunqi.mall.vo.UserLoginVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description:
 * @Created with IntelliJ IDEA.
 * @author: 夏沫止水
 * @create: 2020-04-23 17:05
 **/
@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 用户登录
     * @param form
     * @return
     */
    @Override
    public ResponseVo<UserLoginVo> login(UserLoginForm form) {

        //查询出用户
        User user = userMapper.selectByUsername(form.getUsername());

        //判断用户是否存在
        if (user == null) {
            //用户不存在(返回：用户名或密码错误)
            return ResponseVo.error(ResponseEnum.USERNAME_OR_PASSWORD_ERROR);
        }

        //给前端传过来的密码进行MD5加密
        String md5Password = DigestUtils.md5DigestAsHex(form.getPassword().getBytes(StandardCharsets.UTF_8));

        //如果密码不一致
        if (!user.getPassword().equalsIgnoreCase(md5Password)) {
            //用户不存在(返回：用户名或密码错误)
            return ResponseVo.error(ResponseEnum.USERNAME_OR_PASSWORD_ERROR);
        }

        //将密码设置为空，防止密码泄露
        user.setPassword("");

        UserLoginVo userLoginVo = new UserLoginVo();
        BeanUtils.copyProperties(user,userLoginVo);

        try {
            //将当前类型转换为时间戳
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String createTime = sdf.format(user.getCreateTime());
            String updateTime = sdf.format(user.getUpdateTime());
            Long createLong = Long.valueOf(sdf.parse(createTime).getTime() / 1000);
            Long updateLong = Long.valueOf(sdf.parse(updateTime).getTime() / 1000);
            userLoginVo.setCreateTime(createLong);
            userLoginVo.setUpdateTime(createLong);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return ResponseVo.success(userLoginVo);
    }


    /**
     * 用户注册
     * @param form
     * @return
     */
    @Override
    public ResponseVo register(UserRegisterForm form) {

        //效验用户名是否存在
        User userName = userMapper.selectByUsername(form.getUsername());
        if (userName != null) {
            return ResponseVo.error(ResponseEnum.USER_NAME_EXIST);
        }

        //效验邮箱是否存在
        User userEmail = userMapper.selectByEamil(form.getEmail());
        if (userEmail != null) {
            return ResponseVo.error(ResponseEnum.EMAIL_EXIST);
        }

        //写入数据库
        //给前端传过来的密码进行MD5加密
        String md5Password = DigestUtils.md5DigestAsHex(form.getPassword().getBytes(StandardCharsets.UTF_8));
        User user = new User();
        BeanUtils.copyProperties(form,user);
        user.setPassword(md5Password);
        user.setRole(MallConst.ROOT_PARENT_ID);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        int a = 1/0;
        int userResult = userMapper.insertSelective(user);

        if (userResult <= 0) {
            return ResponseVo.error(ResponseEnum.ERROR);
        }

        return ResponseVo.success();
    }

    /**
     * 获取用户登录信息
     * @param uid
     * @return
     */
    @Override
    public ResponseVo<UserLoginVo> users(Integer uid) {

        User user = userMapper.selectByPrimaryKey(uid);

        //用户不存在
        if (user == null) {
            return ResponseVo.error(ResponseEnum.USER_NAME_NOTEXIST);
        }

        UserLoginVo userLoginVo = new UserLoginVo();
        BeanUtils.copyProperties(user,userLoginVo);

        try {
            //将当前类型转换为时间戳
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String createTime = sdf.format(user.getCreateTime());
            String updateTime = sdf.format(user.getUpdateTime());
            Long createLong = Long.valueOf(sdf.parse(createTime).getTime() / 1000);
            Long updateLong = Long.valueOf(sdf.parse(updateTime).getTime() / 1000);
            userLoginVo.setCreateTime(createLong);
            userLoginVo.setUpdateTime(createLong);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return ResponseVo.success(userLoginVo);
    }
}
